package com.cumt.framework.rest;

/**
 * Created by caicf on 2016/12/30.
 */
public class ApiUrlUtil {

    private ApiUrlUtil() {
    }

    private ApiUrlUtil(String url) {
        this.URL = url;
    }

    private static String URL = "http://[ip]:[port]/[service]";

    public static ApiUrlUtil baseRequestUrl(String ip, String port) {
        String url = URL.replace("[ip]", ip).replace("[port]", port).replace("[service]", "cumt");
        return new ApiUrlUtil(url);
    }

    public ApiUrlUtil path(String path) {
        if (path.startsWith("/")) {
            URL += path;
        } else {
            URL += ("/" + path);
        }
        return this;
    }

    public ApiUrlUtil queryPath(String key, Object value) {
        String url = URL;
        String param = key + "=" + value + ",";
        if (url.contains("?")) {
            url += param;
        } else {
            url = url + "?" + param;
        }
        url = url.substring(0, url.length() - 1);
        URL = url;
        return this;
    }

    public String getUrl() {
        return URL;
    }

    public static void main(String[] args) {
        String url = ApiUrlUtil.baseRequestUrl("127.0.0.1", "5088")
                .path("user")
                .path("deleting")
                .queryPath("id", "39,40,41")
                .getUrl();
        System.out.println(url);
    }
}
